package com.zy.mall.dao;

import com.zy.mall.model.Goods;
import com.zy.mall.model.Spec;
import com.zy.mall.model.Type;
import com.zy.mall.model.vo.TypeGoodsVo;
import com.zy.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

public class GoodDaoImpl implements GoodsDao {
    @Override
    public List<Type> getType() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Type> typeList = null;
        try {
            typeList = runner.query("select * from type", new BeanListHandler<Type>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return typeList;
    }

    @Override
    public List<TypeGoodsVo> getGoodsByType(String typeId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<TypeGoodsVo> goodsVoList = null;
        try {
            goodsVoList = runner.query("select id,img,name,price,typeId,stockNum from goods where typeId = ?",
                    new BeanListHandler<TypeGoodsVo>(TypeGoodsVo.class), typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsVoList;
    }

    @Override
    public int lastInsertId() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        BigInteger query = null;
        try {
            query = (BigInteger) runner.query("select last_insert_id()", new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query.intValue();
    }

    @Override
    public void addGoods(Goods goods) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into goods values (null, ?, ?, ?, ?, ?, ?)",
                    goods.getImg(), goods.getName(), goods.getPrice(),
                    goods.getTypeId(), goods.getStockNum(), goods.getDesc());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addSpecs(List<Spec> specs) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        for (Spec spec : specs) {
            try {
                runner.update("insert into spec values (null, ?, ?, ?, ?)",
                        spec.getSpecName(), spec.getStockNum(), spec.getUnitPrice(), spec.getGoodsId());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int addType(Type name) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Type query = null;
        try {
            query = runner.query("select * from type where name = ?",
                    new BeanHandler<>(Type.class), name.getName());
            if (query == null) {
                return runner.update("insert into type values (null, ?)", name.getName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 10000;
    }
}
